package com.news.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.news.dao.NewsDao;
import com.news.dao.impl.NewsDaoImpl;
import com.news.entity.News;

@WebServlet("/news")
public class NewsServlet extends HttpServlet {
	NewsDao newsdao = new NewsDaoImpl();

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//一种保护，让其它服务器不能访问此应用
		response.setHeader("Access-Control-Allow-Origin", "*");
		System.out.println("来过");
		response.setContentType("text/json;charset=utf-8");
//		response.setContentType("text/plain;charset=utf-8");
//		
	
		
		List<News> newslist=newsdao.getAllNews();
		String json=	JSON.toJSONString(newslist ,SerializerFeature.PrettyFormat  // 表示会换行输出(默认是一行)
                ,SerializerFeature.WriteMapNullValue  // 表示会输出空值(默认空值不序列化)
                ,SerializerFeature.WriteDateUseDateFormat  // 表示会格式化日期(默认是毫秒值)
            ); 
				//JSONObject.toJSONString(newslist);
//		newslist.forEach(System.out::println);
		response.getWriter().append(json);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}
}
